﻿var nowSelectMenu;
var nowpage = 1;
var sumpage = 1;
$(".menu li").on("click", function (e) {
    if (e.target === nowSelectMenu) return;
    nowSelectMenu = e.target;
    console.log("点击成功");
    $(".menu li").removeClass("checked");
    $(this).addClass("checked");
    $(".panel").css("z-index", "0");
    var id = e.target.id;
    switch (id) {
        case "menu-home": {
            $("#panel-home").css("z-index", "1");
            break;
        }
        case "menu-gdmmt": {
            $("#panel-gdmmt").css("z-index", "1");
            break;
        }
    }
})

$("#good-query").on("click", function () {
    var par = $("#input-par").val();
    if (par.length == 0) {
        query(1, 2);
        return;
    }
    query(par, 1);
});

$("#good-add").on("click", function () {
    showAdd_Ifr();
})

//上一页
$("#btn-lastpage").click(function () {
    if (nowpage - 1 <= 0) {
        window.alert("已是最前一页");
        return;
    }
    nowpage--;
    $("#nowpagenum").text(nowpage);
    query(nowpage, 2);
});
//下一页
$("#btn-nextpage").click(function () {
    if (nowpage + 1 > sumpage) {
        window.alert("已是最后一页");
        return;
    }
    nowpage++;
    $("#nowpagenum").text(nowpage);
    query(nowpage, 2);
});
//跳转
$("#btn-skippage").click(function () {
    var v = $("#skipvalue").val();
    if (!v || v.length == 0) return;
    var pnum = parseInt(v);
    if (pnum > sumpage || pnum < 1) {
        window.alert("跨界跳转！")
        return;
    }
    if (pnum == nowpage) return;
    query(pnum, 2);
});

function sendOrder({ oid, jsondata = '', tag = '', succesCallback }) {
    $.ajax({
        url: "Order",
        method: "POST",
        dataType: "json",
        cache: false,
        data: {
            "oid": oid,
            "jsondata": jsondata,
            "tag": tag
        },
        success: succesCallback,
    })
}

function Good() {
    this.gid = 0;
    this.title = "";
    this.price = 0;
    this.typeid = 0;
    this.stock = 0;
    this.Mainimg_url = "";
    this.Subimgs_url = [];
    this.Detimgs_url = [];
    this.brin = "";
    this.enable = true;
}

/**清除商品查询列表 */
function clearGoodsRow() {
    $("#qres tr:gt(0)").remove();
    var obj = new Good({

    });
}

/**
 * 更新商品列表信息
 * @param {...any} par
 */
function updateGoodsRow(...par) {
    clearGoodsRow();
    for (var i = 0; i < par.length; i++) {
        var g = par[i];
        var tr = document.createElement("tr");
        tr.id = g.gid;

        var td_gid = document.createElement("td");
        td_gid.className = "td_gid";
        td_gid.innerHTML = g.gid;

        var td_title = document.createElement("td");
        td_gid.className = "td_title";
        td_gid.innerHTML = g.title;

        var td_price = document.createElement("td");
        td_gid.className = "td_price";
        td_gid.innerHTML = g.price;

        var td_typeid = document.createElement("td");
        td_gid.className = "td_typeid";
        td_gid.innerHTML = g.typeid;

        var td_stock = document.createElement("td");
        td_gid.className = "td_stock";
        td_gid.innerHTML = g.stock;

        var td_brin = document.createElement("td");
        td_gid.className = "td_brin";
        td_gid.innerHTML = g.brin;

        var td_enable = document.createElement("td");
        td_gid.className = "td_enable";
        td_gid.innerHTML = g.enable ? "true" : "false";

        $(tr).append(td_gid)
            .append(td_title)
            .append(td_price)
            .append(td_typeid)
            .append(td_stock)
            .append(td_brin)
            .append(td_enable);

        $("#qres").append(tr);
    }
}

function showAdd_Ifr() {
    var $dom = $("#panel-gdmmt .mask");
    $("#ifr-gd").attr("src", "AddGood");
    $dom.fadeIn(500).css("displa", "flex").css("display", "flex");
}

function showChange_Ifr(guid) {
    var $dom = $("#panel-gdmmt .mask");
    $("#ifr-gd").attr("src", "LookGood?guid=" + guid);
    $dom.fadeIn(500).css("displa", "flex").css("display", "flex");
}

function hide_Ifr() {
    $("#panel-gdmmt .mask").fadeOut(500);
}

function query(value, qtpe) {
    var obj = {
        value: value,
        qtpe: qtpe
    }
    $.ajax({
        url: "/BackStage/QueryGoods",
        dataType: "html",
        data: obj,
        method: "GET",
        success: function (data) {
            $("#panel-gdmmt .btm .page").remove();
            $("#panel-gdmmt .btm").prepend(data);
        },
        error: ajaxError
    })
}

function ajaxError(jqXHR, textStatus, errorThrown) {
    window.alert(`错误信息:
                          状态码: ${jqXHR.status} 
                          状态信息:${jqXHR.state()}
                          错误类型: ${textStatus} 
                          返回信息: ${errorThrown}`);
}

function reQuery() {
    query(nowpage, 2);
    updateSumPage();
}

function updateSumPage() {
    $.post("Order", { "oid": 2 }, function (sum) {
        $("#maxpagenum").text(sum).attr("max",sum);
        sumpage = parseInt(sum);
    }, "text");
}

function removeGood(guid) {
    if (confirm("确认删除吗?")) {
        $.post("Order", { "oid": 1, "tag": guid }, function (data) {
            if (data == "True") {
                window.alert("删除成功!");
                reQuery();
            } else {
                window.alert("删除失败!")
            }
        }, "text");
    }
}